import { Service } from "../service";
import { IProject } from "../model";
export function projectReducer(project: IProject) {
  return project;
}
export const resolvers = {
  Query: {
    projectById: (_: any, { id }: { id: string }) =>
      Service.getProjectByIds([id])[0],
    projects: () => Service.listProjects(),
  },
  Mutation: {
    addProject: (
      _: any,
      { title, parent }: { title: string; parent?: string }
    ) => {
      try {
        const project = Service.addProject({ title, parent });
        Service.save();
        return {
          success: true,
          project,
        };
      } catch (err) {
        return { success: false, message: err.message };
      }
    },
    removeProjectById: (_: any, { id }: { id: string }) => {
      const project = Service.removeProject(id);
      Service.save();
      return {
        success: true,
        project,
      };
    },
    removeProjectByIds: (_: any, { ids }: { ids: string[] }) => {
      const projects = ids.map((id) => Service.removeProject(id));
      Service.save();
      return {
        success: true,
        projects,
      };
    },
  },
};
